package com.wwh.controller;

import com.wwh.mapper.UserMapper;
import com.wwh.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;

    /**
     * 单纯的页面渲染，
     * 不是真正的注册操作
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String registerGet() {
        return "user/register";
    }

    /**
     * 实际的注册操作
     * @param name
     * @param password
     * @param request
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@RequestParam(value = "name") String name,
                           @RequestParam(value = "password") String password,
                           HttpServletRequest request){

        if(!"".equals(name) && !"".equals(password)){
            User user = new User();
            user.setName(name);
            user.setPassword(password);
            int save = userMapper.save(user);
            if(save != 0){
                request.getSession().setAttribute("userInfo", name + " - " + password);
                int userId = user.getId();
                request.getSession().setAttribute("userId", userId);
                return "redirect:listPostBack";
            }
        }
        return "errorPage";
    }

    /**
     * 单纯的页面渲染，
     * 不是真正的登录操作
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginGet() {
        return "user/login";
    }

    /**
     * 实际的登录操作
     * @param name
     * @param password
     * @param request
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestParam(value = "name") String name,
                        @RequestParam(value = "password") String password,
                        HttpServletRequest request){

        if(this.isUser(name, password).equals("true")){
            request.getSession().setAttribute("userInfo", name + " - " + password);
            int userId = this.getUserId(name, password);
            request.getSession().setAttribute("userId", userId);
            return "redirect:listPostBack";
        }else{
            return "errorPage";
        }
    }


    /**
     * 退出登录，
     * 清除session
     * @param session
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("userInfo");
        return "redirect:listPostFront";
    }

    /**
     * 用户是否存在
     * @param name
     * @return
     */
    @RequestMapping(value="/isUser", method = RequestMethod.POST)
    @ResponseBody
    public String isUser( String name, String password){
        User search = userMapper.search(name, password);
        if(search != null){
            return "true";
        }
        return  "false";
    }

    /**
     * 用户是否存在
     * @param name
     * @return
     */
    @RequestMapping(value="/userExists", method = RequestMethod.POST)
    @ResponseBody
    public String userExists(String name){
        User byName = userMapper.getByName(name);
        if(byName != null){
            return "true";
        }
        return  "false";
    }

    /**
     *
     * @param name
     * @param password
     * @return
     */
    public int getUserId(String name, String password){
        int id = 0;
        User search = userMapper.search(name, password);
        if(search != null){
            id = search.getId();
        }
        return id;
    }

}
